<template>
  <!-- ### 物流跟踪页面（LogisticsTracking.vue）
这个页面是用户查看物流信息的入口，包含物流基本信息、物流地图和物流轨迹 -->
  <div class="logistics-tracking-container">
    <h1>物流跟踪</h1>
    
    <div v-if="loading" class="loading-container">
      <p>加载物流信息中...</p>
    </div>
    
    <div v-else-if="error" class="error-container">
      <p>{{ error }}</p>
      <button @click="goBack" class="back-btn">返回</button>
    </div>
    
    <div v-else class="logistics-content">
      <!-- 物流基本信息 -->
      <div class="logistics-info-card">
        <div class="logistics-header">
          <div class="order-info">
            <h2>订单信息</h2>
            <p class="order-number">订单编号: {{ logistics.orderNo }}</p>
          </div>
          <div class="logistics-status">
            <div :class="['status-badge', statusClass]">{{ statusText }}</div>
          </div>
        </div>
        
        <div class="logistics-detail">
          <div class="detail-item">
            <div class="detail-label">物流公司</div>
            <div class="detail-value">{{ logistics.logisticsCompany || '暂无信息' }}</div>
          </div>
          <div class="detail-item">
            <div class="detail-label">物流单号</div>
            <div class="detail-value">{{ logistics.logisticsNumber || '暂无信息' }}</div>
          </div>
          <div class="detail-item">
            <div class="detail-label">预计送达</div>
            <div class="detail-value">{{ logistics.expectedDeliveryTime || '暂无信息' }}</div>
          </div>
        </div>
      </div>
      
      <!-- 收发货信息 -->
      <div class="addresses-card">
        <div class="address-item">
          <div class="address-label">
            <span class="address-icon ship-icon"></span>
            <span>发货地址</span>
          </div>
          <div class="address-value">{{ logistics.shippingAddress }}</div>
        </div>
        <div class="address-item">
          <div class="address-label">
            <span class="address-icon receive-icon"></span>
            <span>收货地址</span>
          </div>
          <div class="address-value">{{ logistics.receivingAddress }}</div>
        </div>
      </div>
      
      <!-- 物流地图组件 -->
      <LogisticsMap
        :order="{
          orderNo: logistics.orderNo,
          shippingCoords: logistics.shippingCoords,
          receivingCoords: logistics.receivingCoords
        }"
        :shipping-address="logistics.shippingAddress"
        :receiving-address="logistics.receivingAddress"
        :logistics-status="logistics.logisticsStatus"
        :logistics-company="logistics.logisticsCompany"
        :logistics-number="logistics.logisticsNumber"
        :expected-delivery-time="logistics.expectedDeliveryTime"
      />
      
      <!-- 物流轨迹 -->
      <div class="logistics-traces-card">
        <h2>物流轨迹</h2>
        
        <div v-if="logistics.logisticsTraces && logistics.logisticsTraces.length" class="traces-timeline">
          <div 
            v-for="(trace, index) in logistics.logisticsTraces" 
            :key="index"
            class="trace-item"
            :class="{ 'active': index === 0 }"
          >
            <div class="trace-time">{{ trace.time }}</div>
            <div class="trace-content">
              <div class="trace-dot"></div>
              <div class="trace-info">
                <div class="trace-action">{{ trace.action }}</div>
                <div class="trace-location">{{ trace.location }}</div>
              </div>
            </div>
          </div>
        </div>
        
        <div v-else class="no-traces">
          <p>暂无物流轨迹信息</p>
        </div>
      </div>
      
      <div class="actions">
        <button @click="goBack" class="back-btn">返回订单详情</button>
        <button @click="refresh" class="refresh-btn">刷新物流</button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { simulateLogisticsInfo } from '../../axios/logistics';
import LogisticsMap from './LogisticsMap.vue';

export default {
  name: 'LogisticsTracking',
  components: {
    LogisticsMap
  },
  setup() {
    const route = useRoute();
    const router = useRouter();
    const orderNo = ref(route.params.orderNo || '');
    const logistics = ref({});
    const loading = ref(true);
    const error = ref('');
    
    // 计算物流状态文本
    const statusText = computed(() => {
      if (!logistics.value || logistics.value.logisticsStatus === undefined) return '未知状态';
      
      const statusMap = {
        0: '待发货',
        1: '运输中',
        2: '已送达',
        3: '已签收'
      };
      
      return statusMap[logistics.value.logisticsStatus] || '未知状态';
    });
    
    // 计算物流状态样式类
    const statusClass = computed(() => {
      if (!logistics.value || logistics.value.logisticsStatus === undefined) return '';
      
      const classMap = {
        0: 'pending',
        1: 'delivering',
        2: 'delivered',
        3: 'signed'
      };
      
      return classMap[logistics.value.logisticsStatus] || '';
    });
    
    // 获取物流信息
    const fetchLogisticsInfo = async () => {
      if (!orderNo.value) {
        error.value = '订单号不能为空';
        loading.value = false;
        return;
      }
      
      try {
        loading.value = true;
        error.value = '';
        
        // 从路由参数中获取收货地址
        const receivingAddress = route.query.address;//111
        
        // 直接使用模拟数据
        console.log('使用模拟物流数据', { receivingAddress });
        // 如果存在收货地址，传递给模拟数据函数
        const mockRes = await simulateLogisticsInfo(orderNo.value, {
          receivingAddress: receivingAddress
        });
        logistics.value = mockRes.data;
        
        // 将地址信息打印出来，便于调试
        console.log('物流信息获取成功', {
          shippingAddress: logistics.value.shippingAddress,
          receivingAddress: logistics.value.receivingAddress
        });
        
        loading.value = false;
      } catch (e) {
        loading.value = false;
        error.value = '获取物流信息失败，请稍后重试';
        console.error('获取物流信息失败:', e);
      }
    };
    
    // 返回上一页
    const goBack = () => {
      router.back();
    };
    
    // 刷新物流信息
    const refresh = () => {
      fetchLogisticsInfo();
    };
    
    // 组件挂载时获取物流信息
    onMounted(() => {
      fetchLogisticsInfo();
    });
    
    return {
      orderNo,
      logistics,
      loading,
      error,
      statusText,
      statusClass,
      goBack,
      refresh
    };
  }
}
</script>

<style scoped>
.logistics-tracking-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  margin-bottom: 24px;
  text-align: center;
  color: #303133;
}

.loading-container, .error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 40px 0;
}

.error-container {
  color: #f56c6c;
}

.back-btn {
  margin-top: 16px;
  padding: 8px 16px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.back-btn:hover {
  background-color: #66b1ff;
}

.logistics-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.logistics-info-card, .addresses-card, .logistics-traces-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.logistics-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.order-info h2 {
  margin: 0 0 8px 0;
  font-size: 18px;
}

.order-number {
  color: #606266;
  font-size: 14px;
}

.status-badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 16px;
  font-size: 14px;
  color: white;
}

.status-badge.pending {
  background-color: #909399;
}

.status-badge.delivering {
  background-color: #409eff;
}

.status-badge.delivered {
  background-color: #67c23a;
}

.status-badge.signed {
  background-color: #67c23a;
}

.logistics-detail {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
}

.detail-item {
  display: flex;
  flex-direction: column;
}

.detail-label {
  color: #909399;
  font-size: 14px;
  margin-bottom: 4px;
}

.detail-value {
  color: #303133;
  font-weight: 500;
}

.addresses-card {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.address-item {
  display: flex;
  flex-direction: column;
}

.address-label {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  color: #606266;
}

.address-icon {
  width: 18px;
  height: 18px;
  margin-right: 8px;
  display: inline-block;
  border-radius: 50%;
}

.ship-icon {
  background-color: #409eff;
}

.receive-icon {
  background-color: #67c23a;
}

.address-value {
  color: #303133;
  font-weight: 500;
  padding-left: 26px;
}

.logistics-traces-card h2 {
  margin-top: 0;
  margin-bottom: 16px;
  font-size: 18px;
}

.traces-timeline {
  position: relative;
}

.traces-timeline::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 120px;
  width: 2px;
  background-color: #e4e7ed;
}

.trace-item {
  display: flex;
  margin-bottom: 16px;
  position: relative;
}

.trace-time {
  width: 120px;
  padding-right: 16px;
  text-align: right;
  color: #909399;
  font-size: 14px;
}

.trace-content {
  flex: 1;
  display: flex;
  align-items: flex-start;
}

.trace-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #c0c4cc;
  position: relative;
  margin-right: 10px;
  margin-top: 5px;
  z-index: 1;
}

.trace-item.active .trace-dot {
  background-color: #409eff;
  width: 12px;
  height: 12px;
}

.trace-info {
  flex: 1;
}

.trace-action {
  font-weight: 500;
  margin-bottom: 4px;
}

.trace-location {
  color: #606266;
  font-size: 14px;
}

.trace-item.active .trace-action,
.trace-item.active .trace-time {
  color: #409eff;
  font-weight: bold;
}

.no-traces {
  color: #909399;
  text-align: center;
  padding: 20px 0;
}

.actions {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 20px;
}

.refresh-btn {
  padding: 8px 16px;
  background-color: #67c23a;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.refresh-btn:hover {
  background-color: #85ce61;
}

@media (max-width: 640px) {
  .logistics-detail {
    grid-template-columns: 1fr;
  }
  
  .traces-timeline::before {
    left: 80px;
  }
  
  .trace-time {
    width: 80px;
  }
}
</style> 